//
//  WJMainTableViewCell.m
//  WJBSApp
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "WJMainTableViewCell.h"
#import "WJTagsCollectionView.h"

//#import <ShareSDK/ShareSDK.h>

#define DetilH self.detailImage.bounds.size.height

@interface WJMainTableViewCell ()<WJTagsCollectionViewDelegate>
@property (nonatomic,strong) WJBaseImageView *headerImage;
@property (nonatomic,strong) WJBaseLable *nameLable;
@property (nonatomic,strong) WJBaseLable *timeLable;
@property (nonatomic,strong) WJBaseImageView *vImage;
@property (nonatomic,strong) WJBaseImageView *vipImageView;
@property (nonatomic,strong) WJBaseLable *titleLable;

@property (nonatomic,retain) WJBaseLable *topComment1;
@property (nonatomic,retain) WJBaseLable *topComment2;
@property (nonatomic,retain) WJBaseLable *topComment3;

@property (nonatomic,retain) WJTagsCollectionView *tagsVC;

@property (nonatomic,retain) WJBaseButton *fowardButton;
@property (nonatomic,retain) WJBaseButton *commentButton;
@property (nonatomic,strong) WJBaseButton *upButton;
@property (nonatomic,strong) WJBaseButton *belowButton;

@end


@implementation WJMainTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    [self createTableViewCellView];
    return self;
}

- (void)createTableViewCellView {
    CGFloat nameLeft = kBSCellMargin + kBSHeaderW + kBSContentMargin;
    CGFloat top = kBSCellMargin;
    
    CGRect headerRect = CGRectMake(kBSCellMargin, top, kBSHeaderW, kBSHeaderW);
    CGRect vRect= CGRectMake(kBSCellMargin + kBSHeaderW - 10, top + kBSHeaderW - 10, 10, 10);
    CGRect nameRect = CGRectMake(nameLeft, top, WIDTH - nameLeft - kBSCellMargin, kBSVipH);
    CGRect timeRect = CGRectMake(nameLeft, top + kBSHeaderW - kBSTimeFont, WIDTH - nameLeft - kBSCellMargin, kBSTimeFont);
    
    ///>  需要优化圆角
    self.headerImage = [[WJBaseImageView alloc]initWithFrame:headerRect];
    self.headerImage.layer.cornerRadius = kBSHeaderW / 2;
    self.headerImage.layer.masksToBounds = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickHeader)];
    self.headerImage.userInteractionEnabled = YES;
    [self.headerImage addGestureRecognizer:tap];
    [self.contentView addSubview:self.headerImage];

    self.vImage = [[WJBaseImageView alloc]initWithFrame:vRect];
    self.vImage.backgroundColor = BSClearColor;
    self.vImage.image = [UIImage imageNamed:@"Profile_AddV_authen.png"];
    [self.contentView addSubview:self.vImage];
    
    self.nameLable = [[WJBaseLable alloc]initWithFrame:nameRect dayBgColor:BSWhiteColor nightBgColor:FORBLACKCOLOR dayTextColor:BSGrayColor nightTextColor:BSWhiteColor];
    self.nameLable.font = [UIFont systemFontOfSize:kBSNameFont];
    [self.contentView addSubview:self.nameLable];
    
    self.vipImageView = WJBaseImageView.new;
    self.vipImageView.backgroundColor = BSClearColor;
    [self.vipImageView sd_setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"dropdown_anim__0001.gif"]];
    [self.nameLable addSubview:self.vipImageView];

    self.timeLable = [[WJBaseLable alloc]initWithFrame:timeRect dayBgColor:BSWhiteColor nightBgColor:FORBLACKCOLOR dayTextColor:BSGrayColor nightTextColor:BSWhiteColor];
    self.timeLable.font = [UIFont systemFontOfSize:kBSTimeFont];
    [self.contentView addSubview:self.timeLable];

    self.titleLable = [[WJBaseLable alloc]init];
    self.titleLable.numberOfLines = 0;
    self.titleLable.font = [UIFont systemFontOfSize:kBSTitleFont];
    [self.contentView addSubview:self.titleLable];

    
#pragma mark -  中间的图片
    ///>  需要优化圆角
    self.detailImage = [[WJBaseImageView alloc]init];
    self.detailImage.userInteractionEnabled = YES;
    self.detailImage.contentMode = UIViewContentModeScaleToFill;
    UITapGestureRecognizer *detailImageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickDetailImage:)];
    [self.detailImage addGestureRecognizer:detailImageTap];
    [self.contentView addSubview:self.detailImage];

    
    _topComment1 = [[WJBaseLable alloc]initWithFrame:CGRectZero dayBgColor:LABLECOLOR nightBgColor:FORBLACKCOLOR dayTextColor:FORBLACKCOLOR nightTextColor:BSWhiteColor];
    _topComment1.font = [UIFont systemFontOfSize:kBSCommentFont];
    _topComment1.numberOfLines = 0;
    [self.contentView addSubview:_topComment1];
    
    _topComment2 = [[WJBaseLable alloc]initWithFrame:CGRectZero dayBgColor:LABLECOLOR nightBgColor:FORBLACKCOLOR dayTextColor:FORBLACKCOLOR nightTextColor:BSWhiteColor];
    _topComment2.font = [UIFont systemFontOfSize:kBSCommentFont];
    _topComment2.numberOfLines = 0;
    [self.contentView addSubview:_topComment2];
    
    _topComment3 = [[WJBaseLable alloc]initWithFrame:CGRectZero dayBgColor:LABLECOLOR nightBgColor:FORBLACKCOLOR dayTextColor:FORBLACKCOLOR nightTextColor:BSWhiteColor];
    _topComment3.font = [UIFont systemFontOfSize:kBSCommentFont];
    _topComment3.numberOfLines = 0;
    [self.contentView addSubview:_topComment3];
    
    self.tagsVC = [[WJTagsCollectionView alloc]init];
    self.tagsVC.cellDelegate = self;
    [self.contentView addSubview:self.tagsVC];
    
    self.upButton = [self createButWithImageNamed:@"mainCellDing" clickName:@"mainCellDingClick"];
    [self.upButton addTarget:self action:@selector(upButtonAction:) forControlEvents:1<<6];
    [self.contentView addSubview:self.upButton];

    self.belowButton = [self createButWithImageNamed:@"mainCellCai" clickName:@"mainCellCaiClick"];
    [self.belowButton addTarget:self action:@selector(belowButtonAction:) forControlEvents:1<<6];
    [self.contentView addSubview:self.belowButton];
    
    self.fowardButton = [self createButWithImageNamed:@"mainCellShare" clickName:@"mainCellShareClick"];
    [self.fowardButton addTarget:self action:@selector(fowardButtonAction:) forControlEvents:1<<6];
    [self.contentView addSubview:self.fowardButton];

    self.commentButton = [self createButWithImageNamed:@"mainCellComment" clickName:@"mainCellCommentClick"];
    [self.commentButton addTarget:self action:@selector(commentButtonAction:) forControlEvents:1<<6];
    [self.contentView addSubview:self.commentButton];
}

- (WJBaseButton *)createButWithImageNamed:(NSString *)imageNamed clickName:(NSString *)clickNamed {
    WJBaseButton *but = [WJBaseButton buttonWithType:UIButtonTypeCustom dayBgColor:BSWhiteColor dayTextColor:BSGrayColor nightBgColor:FORBLACKCOLOR nightTextColor:BSWhiteColor];
    [but setImage:[UIImage imageNamed:imageNamed] forState:UIControlStateNormal];
    [but setImage:[UIImage imageNamed:clickNamed] forState:UIControlStateSelected];
    but.layer.borderWidth = 1;
    but.layer.borderColor = BSLightGrayColor.CGColor;
    but.layer.cornerRadius = 2;
    but.titleLabel.font = BSFont(13);
    CGFloat butW = (kBSContentW - kBSCellMargin * 3) / 4.0f;
    but.imageRect = CGRectMake(butW / 2.0f - 25, 5, 20, 20);
    but.titleRect = CGRectMake(butW / 2.0f + 5, 0, butW / 2.0f - 5, kBSButH);
    return but;
}

- (void)setLayout:(WJMianLayout *)layout {
    _layout = layout;
    WJMainModel *model = layout.model;
    
    [self.headerImage sd_setImageWithURL:[NSURL URLWithString:model.user.header[0]]];
    self.nameLable.text = model.user.name;
    self.timeLable.text = model.passtime;
    self.titleLable.text = model.text;
    
    // 判断是否是V
    if (model.user.is_v) {
        self.vImage.hidden = NO;
    }else{
        self.vImage.hidden = YES;
    }
    
    // 判断是否是VIP
    if (model.user.is_vip) {
        self.vipImageView.hidden = NO;
        self.nameLable.textColor = BSRedColor;
    }else{
        self.vipImageView.hidden = YES;
        self.nameLable.textColor = BSGrayColor;
    }
    
    if (![WJBSTool isBlankString:layout.imageName]) {
        [_detailImage sd_setImageWithURL:[NSURL URLWithString:layout.imageName]];
//        if (_layout.isScale && !isnan(layout.imageScale)) {
//            _detailImage.layer.contentsRect = CGRectMake(0, 0, 1, layout.imageScale);
//        } else {
//            _detailImage.layer.contentsRect = CGRectMake(0, 0, 1, 1);
//        }
    }
    
    _topComment1.frame = CGRectZero;
    _topComment2.frame = CGRectZero;
    _topComment3.frame = CGRectZero;

    if (layout.commentHeights.count == 1) {
        _topComment1.text = model.topComments[0].showText;
        CGFloat comment1H = layout.commentHeights[0].floatValue;
        _topComment1.frame = CGRectMake(kBSCellMargin, layout.commentY, kBSContentW, comment1H);
    } else if (layout.commentHeights.count == 2) {
        _topComment1.text = model.topComments[0].showText;
        _topComment2.text = model.topComments[1].showText;
        CGFloat comment1H = layout.commentHeights[0].floatValue;
        CGFloat comment2H = layout.commentHeights[1].floatValue;
        _topComment1.frame = CGRectMake(kBSCellMargin, layout.commentY, kBSContentW, comment1H);
        _topComment2.frame = CGRectMake(kBSCellMargin, layout.commentY + comment1H + kBSContentMargin, kBSContentW, comment2H);
    } else if (layout.commentHeights.count == 3) {
        _topComment1.text = model.topComments[0].showText;
        _topComment2.text = model.topComments[1].showText;
        _topComment3.text = model.topComments[2].showText;
        CGFloat comment1H = layout.commentHeights[0].floatValue;
        CGFloat comment2H = layout.commentHeights[1].floatValue;
        CGFloat comment3H = layout.commentHeights[2].floatValue;
        _topComment1.frame = CGRectMake(kBSCellMargin, layout.commentY, kBSContentW, comment1H);
        _topComment2.frame = CGRectMake(kBSCellMargin, layout.commentY + comment1H + kBSContentMargin, kBSContentW, comment2H);
        _topComment3.frame = CGRectMake(kBSCellMargin, layout.commentY + comment1H + comment2H + kBSContentMargin * 2, kBSContentW, comment3H);
    }
    
    
    self.tagsVC.array = model.tags;
    
    [self.upButton setTitle:model.up forState:UIControlStateNormal];
    [self.belowButton setTitle:model.down forState:UIControlStateNormal];
    [self.fowardButton setTitle:model.forward forState:UIControlStateNormal];
    [self.commentButton setTitle:model.comment forState:UIControlStateNormal];

    _vipImageView.frame = _layout.vipRect;
    _titleLable.frame = _layout.titleRect;
    _detailImage.frame = _layout.imageRect;
    _tagsVC.frame = _layout.tagRect;
    
    _upButton.frame = _layout.upButRect;
    _belowButton.frame = _layout.downButRect;
    _fowardButton.frame = _layout.forwardButRect;
    _commentButton.frame = _layout.commentButRect;
}


#pragma mark - 点击进入分享界面
- (void)fowardButtonAction:(WJBaseButton *)button {
    if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(mainTableViewCellClickFowardWithLayout:)]) {
        [self.cellDelegate mainTableViewCellClickFowardWithLayout:_layout];
    }
}

#pragma mark - 点击进入评论界面
- (void)commentButtonAction:(WJBaseButton *)button {
    if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(mainTableViewCellClickCommentWithLayout:)]) {
        [self.cellDelegate mainTableViewCellClickCommentWithLayout:_layout];
    }
}

- (void)upButtonAction:(WJBaseButton *)button {
    button.selected = !button.isSelected;
}

- (void)belowButtonAction:(WJBaseButton *)button {
    button.selected = !button.isSelected;

}
#pragma mark - 点击头像进入关注界面
- (void)clickHeader {
    if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(mainTableViewCellClickHeaderWithLayout:)]) {
        [self.cellDelegate mainTableViewCellClickHeaderWithLayout:_layout];
    }
}

- (void)clickDetailImage:(UITapGestureRecognizer *)tap {
    switch (_layout.style) {
        case WJMianLayoutImage:
            if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(mainTableViewCellClickImageWithLayout:imageView:isPush:)]) {
                [self.cellDelegate mainTableViewCellClickImageWithLayout:_layout imageView:(UIImageView *)tap.view isPush:YES];
            }
            break;
        case WJMianLayoutVideo: {
            WJVideoPlayView *playerView = [WJVideoPlayView sharedPlayView];
            playerView.frame = self.detailImage.bounds;
            [self.detailImage addSubview:playerView];
            playerView.playerUrl = [NSURL URLWithString:_layout.model.video.video[1]];
            if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(mainTableViewCellClickVideoWithLayout:)]) {
                [self.cellDelegate mainTableViewCellClickVideoWithLayout:_layout];
            }
        }
            break;
        case WJMianLayoutAudio: {
            if (_layout.model.audio.audio && _layout.model.audio.audio.count > 0) {
                WJAudioPlayView *playerView = [WJAudioPlayView sharedPlayView];
                playerView.model = _layout.model.audio;
                [BSKeyWindow addSubview:playerView];
            }
        }
            break;
        case WJMianLayoutHtml:
            if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(mainTableViewCellClickHtmlWithLayout:)]) {
                [self.cellDelegate mainTableViewCellClickHtmlWithLayout:_layout];
            }
            break;
        case WJMianLayoutGif:
            if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(mainTableViewCellClickGifWithLayout:)]) {
                [self.cellDelegate mainTableViewCellClickGifWithLayout:_layout];
            }
            break;
        default:
            break;
    }
}

- (void)tagsCollectionViewdidSelectItemWithModel:(Tag *)model {
    if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(mainTableViewCellClickTagWithModel:)]) {
        [self.cellDelegate mainTableViewCellClickTagWithModel:model];
    }
}
@end
